Skip to main content

Python OrderedDict

banner-python

🐍 Python's OrderedDict – Order With a Splash of Sass​

Ever felt like your regular dictionary just didn’t respect the order of things? Like that one coworker who always jumps the lunch queue? Well, say hello to OrderedDict – the VIP list of dictionaries where order is everything.

Let’s dive into the wild world of OrderedDict with examples, jokes, and a sprinkle of Pythonic charm.


🎯 Quick Reference​

from collections import OrderedDict

user = OrderedDict(name='sujit', id='100', email='admin@gmail.com')

# Iteration
for key in user:
print(key + ":" + user[key])

# Add a Key at the last
user['location'] = 'India'

# Update a Key
user['email'] = 'admin@fossgurusujit.com'

# Delete a Key
del user['email']

# Pop item from last and start
user.popitem() # from last of dictionary
user.popitem(last=False) # from start of dictionary

# Move item to last and start
user.move_to_end('id') # move to last
user.move_to_end('id', False) # move to start

1. 🧠 What in the World is an OrderedDict?​

An OrderedDict is like that super organized friend who remembers everything – in order! It’s a subclass of dict that keeps keys in the exact order you shoved them in. Unlike the old-school dict (pre-3.7), which used to just throw your data around like confetti at a wedding.

⚠️ Keeping order does come at a cost – 50% more memory, but hey, it’s the price of class.


2. πŸ› οΈ Creating Your Fancy OrderedDict​

Let’s summon one from the depths of collections:

from collections import OrderedDict

πŸŽ‰ Now create one:

user = OrderedDict()
user['name'] = 'sujit'
user['id'] = 100
user['email'] = 'admin@gmail.com'

print(user)
# OrderedDict([('name', 'sujit'), ('id', 100), ('email', 'admin@gmail.com')])

Or flex your constructor muscles:

user = OrderedDict(name='sujit', id=100, email='admin@gmail.com')
print(user)
# OrderedDict([('name', 'sujit'), ('id', 100), ('email', 'admin@gmail.com')])

πŸ”§ Need a default value for multiple keys? fromkeys() got you:

keys = ['id', 'name', 'email']
user = OrderedDict.fromkeys(keys, 0)

3. πŸ” Marching Through an OrderedDict Like a Boss​

Loop it like a pro, folks:

user = OrderedDict(name='sujit', id="100", email='admin@gmail.com')

# Using keys directly
for key in user:
print(key + ":" + user[key])

for key in user.keys():
print(key + ":" + user[key])

# Using items (because you're fancy)
for key, value in user.items():
print(key + ":" + value)

Output like poetry:

name:sujit
id:100
email:admin@gmail.com
name:sujit
id:100
email:admin@gmail.com
name:sujit
id:100
email:admin@gmail.com

4. ✍️ Add, Zap, and Morph Keys Like a Dict Wizard​

Because OrderedDict is mutable, you can do all the usual key sorcery:

user = OrderedDict(name='sujit', id=100, email='admin@gmail.com')
user['location'] = 'India'
print(user)
# OrderedDict([('name', 'sujit'), ('id', 100), ('email', 'admin@gmail.com'), ('location', 'India')])

πŸ§ͺ Updating values won’t mess up the order:

user['email'] = 'admin@fossgurusujit.com'

πŸ—‘οΈ Delete that key like it never existed:

del user['email']

5. 🌐 JSON – Now With Preserved Sass​

Need that order preserved in JSON too? OrderedDict won’t let you down:

from collections import OrderedDict
import json

d = OrderedDict()
d['how'] = 1
d['to'] = 2
d['do'] = 3
d['in'] = 4
d['java'] = 5

json.dumps(d)
# '{"how": 1, "to": 2, "do": 3, "in": 4, "java": 5}'

Even your JSON wears a tuxedo now.


6. βš–οΈ OrderedDict vs. dict – Let’s Rumble​

Let’s settle this showdown:

6.1 🎯 move_to_end() – Because Sometimes Keys Need a Vacation​

Want to move keys around like a boss? You got this.

user = OrderedDict(name='sujit', id="100", email='admin@gmail.com')
user.move_to_end('id')
# Now 'id' is at the end
user.move_to_end('id', False)
# Now 'id' is back at the front
print(user)
# OrderedDict([('id', '100'), ('name', 'sujit'), ('email', 'admin@gmail.com')])

6.2 🧹 popitem() – Gentle Kicking Out From Either End​

In a regular dict, popitem() just tosses the last item. OrderedDict? It gives you options, like a fancy coffee shop.

user = OrderedDict(name='sujit', id="100", email='admin@gmail.com')
print(user.popitem()) # ('email', 'admin@gmail.com')
print(user.popitem(last=False)) # ('name', 'sujit')

7. πŸŽ“ That’s a Wrap​

In this whirlwind of a tutorial, you’ve learned:

  • What an OrderedDict is (fancy!)
  • How to create one (with style!)
  • How to loop, update, and delete (like a pro!)
  • Differences with regular dict (OrderedDict wins on class)

Keep your dictionaries classy and in order.
Happy Coding, Pythonistas! πŸπŸ’»